package flc.ast.dialog;

import static com.blankj.utilcode.util.ActivityUtils.startActivity;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;

import flc.ast.R;

import flc.ast.activity.PriAlbumActivity;
import flc.ast.databinding.DialogInputPasswordBinding;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseSmartDialog;
import stark.common.basic.utils.SPUtil;

public class InputPasswordDialog extends BaseSmartDialog<DialogInputPasswordBinding> implements View.OnClickListener {

    private String password = "";
    private int inputPos = 0;
    private boolean isInput = false;


    public InputPasswordDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_input_password;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvPasswordReset.setOnClickListener(this);
        mDataBinding.ivPasswordClear.setOnClickListener(this);
        mDataBinding.tvPasswordForget.setOnClickListener(this);
        mDataBinding.ivPasswordConfirm.setOnClickListener(this);
        //设置密码
        mDataBinding.rlNum0.setOnClickListener(this);
        mDataBinding.rlNum1.setOnClickListener(this);
        mDataBinding.rlNum2.setOnClickListener(this);
        mDataBinding.rlNum3.setOnClickListener(this);
        mDataBinding.rlNum4.setOnClickListener(this);
        mDataBinding.rlNum5.setOnClickListener(this);
        mDataBinding.rlNum6.setOnClickListener(this);
        mDataBinding.rlNum7.setOnClickListener(this);
        mDataBinding.rlNum8.setOnClickListener(this);
        mDataBinding.rlNum9.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rlNum0://按钮0
                clickNum(mDataBinding.ivNum0Bg, "0");
                break;
            case R.id.rlNum1://按钮1
                clickNum(mDataBinding.ivNum1Bg, "1");
                break;
            case R.id.rlNum2://按钮2
                clickNum(mDataBinding.ivNum2Bg, "2");
                break;
            case R.id.rlNum3://按钮3
                clickNum(mDataBinding.ivNum3Bg, "3");
                break;
            case R.id.rlNum4://按钮4
                clickNum(mDataBinding.ivNum4Bg, "4");
                break;
            case R.id.rlNum5://按钮5
                clickNum(mDataBinding.ivNum5Bg, "5");
                break;
            case R.id.rlNum6://按钮6
                clickNum(mDataBinding.ivNum6Bg, "6");
                break;
            case R.id.rlNum7://按钮7
                clickNum(mDataBinding.ivNum7Bg, "7");
                break;
            case R.id.rlNum8://按钮8
                clickNum(mDataBinding.ivNum8Bg, "8");
                break;
            case R.id.rlNum9://按钮9
                clickNum(mDataBinding.ivNum9Bg, "9");
                break;
            case R.id.ivPasswordClear://清除
                mDataBinding.tvPasswordInputTip.setTextColor(Color.parseColor("#BFBFC3"));
                mDataBinding.tvPassword1.setBackgroundResource(R.drawable.shape_password_off);
                mDataBinding.tvPassword2.setBackgroundResource(R.drawable.shape_password_off);
                mDataBinding.tvPassword3.setBackgroundResource(R.drawable.shape_password_off);
                mDataBinding.tvPassword4.setBackgroundResource(R.drawable.shape_password_off);
                mDataBinding.tvPassword5.setBackgroundResource(R.drawable.shape_password_off);
                mDataBinding.tvPassword6.setBackgroundResource(R.drawable.shape_password_off);
                password = "";
                inputPos = 0;
                break;
            case R.id.tvPasswordReset:
            case R.id.tvPasswordForget://忘记密码
                dismiss();
                listener.forgetPassword();
                break;
            case R.id.ivPasswordConfirm://确定
                isInputRight();
                break;
        }
    }

    //判断输入密码是否正确
    private void isInputRight() {
        String spPassword = SPUtil.getString(getContext(), Content.myPassword, "");
        if (password.equals(spPassword)) {
            dismiss();
            startActivity(PriAlbumActivity.class);
        } else {
            isInput = true;
            mDataBinding.tvPasswordInputTip.setText(R.string.password_error);
            mDataBinding.tvPasswordInputTip.setTextColor(Color.parseColor("#FF5050"));
            mDataBinding.tvPassword1.setBackgroundResource(R.drawable.shape_password_error);
            mDataBinding.tvPassword2.setBackgroundResource(R.drawable.shape_password_error);
            mDataBinding.tvPassword3.setBackgroundResource(R.drawable.shape_password_error);
            mDataBinding.tvPassword4.setBackgroundResource(R.drawable.shape_password_error);
            mDataBinding.tvPassword5.setBackgroundResource(R.drawable.shape_password_error);
            mDataBinding.tvPassword6.setBackgroundResource(R.drawable.shape_password_error);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mDataBinding.tvPasswordInputTip.setText(R.string.input_password_tip);
                    mDataBinding.tvPasswordInputTip.setTextColor(Color.parseColor("#BFBFC3"));
                    mDataBinding.tvPassword1.setBackgroundResource(R.drawable.shape_password_off);
                    mDataBinding.tvPassword2.setBackgroundResource(R.drawable.shape_password_off);
                    mDataBinding.tvPassword3.setBackgroundResource(R.drawable.shape_password_off);
                    mDataBinding.tvPassword4.setBackgroundResource(R.drawable.shape_password_off);
                    mDataBinding.tvPassword5.setBackgroundResource(R.drawable.shape_password_off);
                    mDataBinding.tvPassword6.setBackgroundResource(R.drawable.shape_password_off);
                    password = "";
                    inputPos = 0;
                    isInput = false;
                }
            }, 500);
        }
    }

    //按下密码
    private void clickNum(ImageView view, String num) {
        if (isInput || inputPos == 6) return;
        isInput = true;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                password = password + num;
                inputPos++;
                showPassword();
                isInput = false;
            }
        }, 100);
    }

    //管理输入状态
    private void showPassword() {
        switch (inputPos) {
            case 1:
                mDataBinding.tvPassword1.setBackgroundResource(R.drawable.shape_password_on);
                break;
            case 2:
                mDataBinding.tvPassword2.setBackgroundResource(R.drawable.shape_password_on);
                break;
            case 3:
                mDataBinding.tvPassword3.setBackgroundResource(R.drawable.shape_password_on);
                break;
            case 4:
                mDataBinding.tvPassword4.setBackgroundResource(R.drawable.shape_password_on);
                break;
            case 5:
                mDataBinding.tvPassword5.setBackgroundResource(R.drawable.shape_password_on);
                break;
            case 6:
                mDataBinding.tvPassword6.setBackgroundResource(R.drawable.shape_password_on);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        isInputRight();
                    }
                }, 500);
                break;
        }
    }


    private Listener listener;

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public interface Listener {
        void forgetPassword();
    }
}
